<html>
<head><title>Sound Test</title></head>
<body>
<script src="../tma.js"></script>
<script>
tma.extlibs = [ 'ext/gl-matrix.js', 'ext/mv/MajVj.js' ];
tma.onload = function () {
  var snd;
  var songs;
  var gain = 1.0;
  var ch = 0;

  document.body.addEventListener('keydown', function (e) {
    switch (e.which) {
      case 49:  // 1
        snd.play(songs[0], ch);
        break;
      case 50:  // 2
        snd.play(songs[1], ch);
        break;
      case 51:  // 3
        snd.play(songs[2], ch);
        break;
      case 52:  // 4
        snd.play(songs[3], ch);
        break;
      case 53:  // 5
        snd.play(songs[4], ch);
        break;
      case 88:  // x
        ch = 1;
        break;
      case 90:  // z
        ch = 0;
        break;
      case 187:  // +
        gain += 0.01;
        if (gain > 1.0)
          gain = 1.0;
        snd.setGain(gain, ch);
        break;
      case 189:  // -
        gain -= 0.01;
        if (gain < 0.0)
          gain = 0.0;
        snd.setGain(gain, ch);
        break;
      default:
        tma.log(e.which);
        break;
    }
  });

  var error = function (e) {
    console.error('plugin load error: ' + e.stack);
  };

  var main = function (data) {
    tma.log(data.length + ' sounds loaded.');
    songs = data;
    snd.play(songs[0]);
  };
  var start = function () {
    tma.log('loading sound data...');
    var vj = new MajVj(0, 0);
    snd = vj.create('misc', 'sound', { channel: 2 });
    var site = 'data/0004/';
    Promise.all([
        snd.fetch(site + 'track01.mp3'),
        snd.fetch(site + 'track03.mp3'),
        snd.fetch(site + 'track11.mp3'),
        snd.fetch(site + 'track13.mp3'),
        snd.fetch(site + 'track16.mp3')
    ]).then(main, error);
  };

  MajVj.loadAllPlugins().then(start, error);
};
</script>
</body>
</html>
